package com.wokeMy.view.channe.changjie;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class ChangjieRzActivity extends BaseActivity implements View.OnClickListener{
    ImageView back_cjie_iv;
    RelativeLayout khcard_cjie_rl;
    Button kait_cjie_bt;
    TextView khk_cjie_tv;
    EditText lhk_cjie_et;

    Data_bindcarlist listdata;
    UnionPayChannel unionPayChannel;
    String pay_card_no,bankCode;
    String user_id;
    int way_id,otherWay_id;// 通道 另外判断通道  77畅捷山高 78 商旅；
    String controller;
    int payWay;
    String useName,merchantName,merchantBillName,legalPersonID,merchantPersonPhone;
    private float money;
    Dialog dialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_changjie_rz);
    }

    @Override
    public void initActivity() {
        back_cjie_iv.setOnClickListener(this);
        khcard_cjie_rl.setOnClickListener(this);
        kait_cjie_bt.setOnClickListener(this);
    }

    @Override
    public void initView() {
        back_cjie_iv = myFindViewById(R.id.back_cjie_iv);
        khcard_cjie_rl = myFindViewById(R.id.khcard_cjie_rl);
        khk_cjie_tv = myFindViewById(R.id.khk_cjie_tv);
        kait_cjie_bt = myFindViewById(R.id.kait_cjie_bt);
        lhk_cjie_et = myFindViewById(R.id.lhk_cjie_et);
    }

    @Override
    public void initData() {
        MyApp myApp=(MyApp)getApplication();
        Intent intent=getIntent();
        Datas_load datas_load=myApp.getDatas_load();
        Log.e("legalPersonName",datas_load.toString());
        user_id=datas_load.getId();
        unionPayChannel = (UnionPayChannel)(intent.getSerializableExtra("UnionPayChannel"));
        money=intent.getFloatExtra ("money", 0.01f);
        Log.e("unionPayChannel",unionPayChannel.toString());
        way_id=unionPayChannel.getWay();
        otherWay_id=(way_id==77)? 78:77;
        controller =unionPayChannel.getController();
        payWay=unionPayChannel.getKind();
        String name="名字为空";
        if (!datas_load.getUser_nicename().equals("")){
            name=datas_load.getUser_nicename();
        }else {
            name=datas_load.getCom_name();
        }
        useName=name;
        merchantName=name+"的商户";
        merchantBillName=name+"的商户";
        legalPersonID=datas_load.getUser_no();
        merchantPersonPhone=datas_load.getUser_login();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_cjie_iv:
                finish();
                break;
            case R.id.khcard_cjie_rl:
                Intent intent = new Intent (ChangjieRzActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "rhlz");
                startActivityForResult (intent, Constant.REQUEST_RHLZ);
                break;
            case R.id.kait_cjie_bt:


//                bankCode=lhk_cjie_et.getText().toString().trim();
                bankCode="111111";
                if (pay_card_no==null||pay_card_no.equals("")){
                    Toast.makeText(this,"请选择结算卡",Toast.LENGTH_SHORT).show();
                }else if(bankCode==null||bankCode.equals("")) {
                    Toast.makeText(this,"请填写联行号",Toast.LENGTH_SHORT).show();
                }else{
                    if (!fastClick()) {
                        dialog = Util.createLoadingDialog(ChangjieRzActivity.this);
                        rhlzSelft();
                    }
                }

                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==Constant.RESPONSE_RHLZ){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=listdata.cardno;
            khk_cjie_tv.setText(pay_card_no);
        }
    }
    //    畅捷入驻（本通道）
    public void rhlzSelft(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);   //通道
        params.put("bankAccountNo",pay_card_no);
        params.put("bankChannelNo",bankCode);
        params.put("merName",merchantName);
        params.put("merAbbr",merchantBillName);
        // params.put("creditCardNo","6225757545273378");
        Log.e(" 畅捷入驻（本通道）params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CJRZ_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("畅捷入驻（本通道）/////////", result);
                    try {

                        JSONObject  jsonObject =new JSONObject(result);
                        if (jsonObject.getString("flag").equals("unknown")){
                            Toast.makeText(ChangjieRzActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
                            Util.closeDialog(dialog);
                        }else {
                            JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                            String respcode=jsonObject1.getString("code");
                            if (respcode.equals("00")){
                                cjFeilv();
                            }else if(respcode.equals("2015")){
                                Toast.makeText(ChangjieRzActivity.this,"商户已经入驻过",Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(ChangjieRzActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    //    畅捷入驻（另外通道）
    public void rhlzOther(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",78);   //通道
        params.put("bankAccountNo",pay_card_no);
        params.put("bankChannelNo",bankCode);
        params.put("merName",merchantName);
        params.put("merAbbr",merchantBillName);
        // params.put("creditCardNo","6225757545273378");
        Log.e(" 畅捷入驻（另外通道）params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CJRZ_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("畅捷入驻（另外通道）/////////", result);
//                    try {
//                        JSONObject  jsonObject =new JSONObject(result);
//                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
//                        String respcode=jsonObject1.getString("respcode");
//                        if (respcode.equals("0000")){
//                            Toast.makeText(ChangjieRzActivity.this,"入驻成功",Toast.LENGTH_LONG).show();
//                            finish();
//                        }else if(respcode.equals("2015")){
//                            Toast.makeText(ChangjieRzActivity.this,"商户已经入驻过",Toast.LENGTH_SHORT).show();
//                        }else {
//                            Toast.makeText(ChangjieRzActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    //    畅捷新增另外一个费率
    public void cjFeilv(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",otherWay_id);   //通道 另外一个的费率
        params.put("bankAccountNo",pay_card_no); //结算卡
        params.put("changeType","3");//1 交易费率变更 2 银行卡信息变更 3 交易费率新增 4 提现费率变更
        // params.put("creditCardNo","6225757545273378");
        Log.e(" 畅捷新增另外一个费率params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CJXZFR_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("畅捷新增另外一个费率/////////", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                        String respcode=jsonObject1.getString("code");
                        if (respcode.equals("00")){
                            Toast.makeText(ChangjieRzActivity.this,"开通成功",Toast.LENGTH_LONG).show();
                            finish();
                            Intent intent2 = new Intent();
                            intent2.setClass (ChangjieRzActivity.this,
                                    CjieKjzfActivity.class);
                            intent2.putExtra("UnionPayChannel",unionPayChannel);
                            intent2.putExtra ("money",money);
                            startActivity (intent2);
                        }else if(respcode.equals("2015")){
                            Toast.makeText(ChangjieRzActivity.this,"商户已经入驻过",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(ChangjieRzActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }


}
